//
//  QZCountyVc.m
//  WeiWeiHaircut
//
//  Created by 上官惠阳 on 15/12/15.
//  Copyright © 2015年 超群. All rights reserved.
//

#import "QZCountyVc.h"
#import "QZEditAddressVc.h"

@interface QZCountyVc ()

@end

@implementation QZCountyVc
{
    NSString *_myLoction;//我的位置
    
    BMKLocationService* _locService;//定位用的
    BMKGeoCodeSearch* _geocodesearch;       //检索服务
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self titleWithSting:@"地区"];
    
    [self addBtnOnNavWithTitle:@"取消" andImageName:@"返回" andTarget:self andAction:@selector(backAction) andFrame:CGRectMake(0, 0, 45, 20) andDirection:left];
    
    [self createTableViewWithPlainStyle:NO andSeparatorStyleNone:NO andBackGroudImageName:nil];
    [self registCellWithClassName:[UITableViewCell class] addIdentifier:@"cell" addHeight:44];
    
    [self loadData];
    
    [self initSome];
    [self startLocation];
}
//初始化一些
-(void)initSome
{
    _locService = [[BMKLocationService alloc]init];
    _geocodesearch = [[BMKGeoCodeSearch alloc]init];
}
-(void)viewWillAppear:(BOOL)animated {

    _locService.delegate = self;
    _geocodesearch.delegate = self;
}
-(void)viewWillDisappear:(BOOL)animated{
    _locService.delegate = nil;
    _geocodesearch.delegate = nil;
}
// 开始定位
-(void)startLocation {
    [_locService startUserLocationService];
}
#pragma mark BMKLocationServiceDelegate
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    CLLocationCoordinate2D coord;
    coord.latitude = userLocation.location.coordinate.latitude;
    coord.longitude = userLocation.location.coordinate.longitude;
    
    //反geo检索信息类
    BMKReverseGeoCodeOption *option = [[BMKReverseGeoCodeOption alloc]init];
    option.reverseGeoPoint = coord;
    [_geocodesearch reverseGeoCode:option];
}
#pragma mark BMKGeoCodeSearchDelegate
//反向地理编码 获取自己所在的城市
-(void) onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
{
    if (error == 0) {
        if (result.addressDetail.city != nil) {
            _myLoction = result.address;
            [self.tableSource replaceObjectAtIndex:0 withObject:@[_myLoction]];
            [self.tableView reloadData];
        }
    }
}
-(void)loadData
{
    [self.tableSource addObject:@[@""]];
    [self.tableSource addObject:@[@"中国"]];
    
    [self.tableView reloadData];
}
-(void)loadCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }else{
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    
    cell.textLabel.text = self.tableSource[indexPath.section][indexPath.row];
}
-(void)actionAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1 && indexPath.row == 0) {
        QZEditAddressVc *editVc = [[QZEditAddressVc alloc]init];
        editVc.isAddAddress = self.isAddAddress;
        [self.navigationController pushViewController:editVc animated:YES];
    }
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return @"定位位置";
    }else{
        return @"全部";
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.000000001;
}
-(void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
